package dr.joketalker.dao.json;


import java.util.Date;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import dr.joketalker.dao.FactoryDAO;
import dr.joketalker.dao.JokeDAO;
import dr.joketalker.entity.Joke;
import dr.joketalker.entity.JokeEvaluation;
import dr.joketalker.entity.User;

public class JSONJoke extends JSONGeneric<Joke> implements JokeDAO {


	public static final String TITLE 			= "TITLE";
	public static final String DESCRIPTION 	= "DESCRIPTION";
	public static final String USER_ID 		= "USER_ID";
	public static final String CREATED 		= "CREATED";
	public static final String CATEGORY 		= "CATEGORY";
	public static final String LANGUAGE 		= "LANGUAGE";
	public static final String LINKYT			= "LINKYT";
	public static final String LINKFTP			= "LINKFTP";
	public static final String POINTS			= "POINTS";
	public static final String CLICKS			= "CLICKS";
	

	
	public JSONJoke(){
		TABLENAME = "JOKE";
	}
	
	
	@Override//insert
	public List<NameValuePair> getListParams(Joke joke) {
		List<NameValuePair> listParams = super.getListParams(joke);
		
		listParams.add(new BasicNameValuePair(TITLE, joke.getTitle()));		
		listParams.add(new BasicNameValuePair(DESCRIPTION, joke.getDescription()));
		
		listParams.add(new BasicNameValuePair(USER_ID, joke.getUser().getId()+""));		
		listParams.add(new BasicNameValuePair(CREATED, new Date().toString()));
		
		listParams.add(new BasicNameValuePair(CATEGORY, joke.getCategory()));		
		listParams.add(new BasicNameValuePair(LANGUAGE, joke.getLanguage()));
		
		listParams.add(new BasicNameValuePair(LINKYT, joke.getLinkYt()));		
		listParams.add(new BasicNameValuePair(LINKFTP, joke.getLinkFtp()));
		
		listParams.add(new BasicNameValuePair(POINTS, joke.getPoints()+""));
		
		listParams.add(new BasicNameValuePair(CLICKS, joke.getClicks()+""));
		
		return listParams;
	}
	

@Override
public Joke getEntityInstance() {
	// TODO Auto-generated method stub
	return new Joke();
}

@Override
public Joke buildEntityFromJson(JSONObject jsonO) {
	Joke joke = new Joke();
	try{
		joke.setId(jsonO.getLong(ID));
		joke.setTitle(jsonO.getString(TITLE));
		joke.setDescription(jsonO.getString(DESCRIPTION));
		joke.setCreated (jsonO.getString(CREATED));
		joke.setLanguage(jsonO.getString(LANGUAGE));
		joke.setCategory(jsonO.getString(CATEGORY));
		joke.setLinkYt(jsonO.getString(LINKYT));
		joke.setLinkFtp(jsonO.getString(LINKFTP));
		
		joke.setClicks(jsonO.getLong(CLICKS));
		joke.setPoints(jsonO.getLong(POINTS));

		long userId			= jsonO.getLong(USER_ID);
	
		User cUser = FactoryDAO.getUserDao().read(userId);
		joke.setUser(cUser);
		
/*todo
		List<JokeEvaluation> listEvaluation =  FactoryDAO.getJokeEvaluationDao().readAllWhere(JSONJokeEvaluation.JOKE_ID, joke.getId()+"",0);
		joke.setListJokeEvualation(listEvaluation);
	*/	
	
	}catch(Exception ex){
		//myDebug.v(ex.toString());
	}
	return joke;
}






	
}
